<?php
namespace Admin\Model;
use Think\Model;
class UserModel extends Model {
	public function add_user(){//官方授权
		$data=I('');



		//更新操作
		$user_id=$data["user_id"];
		unset($data["user_id"]);



		//检测手机/微信是否存在
		$status=$this->check_user($data['phone'],$data['wechat'],$user_id);
		if($status){return "手机号或微信号已存在";exit;} 

        if(!$user_id)
        {
        	$offical=65;
			$code=chr($offical);
			$ychar="0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,i,s,t,u,v,w,x,y,z";
			$list=explode(",",$ychar);
			for($i=0;$i<8;$i++){
				$randnum=rand(0,35); 
				$authnum.=$list[$randnum];
			}
			$data['password']=md5($authnum);
			$info=$this->where("shouquan='{$code}'")->count();
			while($info==1){
				$offical=$offical+1;
				if($offical>91){
					return "官方名额已授权完！";
					exit;
				}
				$code=chr($offical);
				$info=$this->where("shouquan='{$code}'")->count();
			}
			$data['shouquan']=$code;		

			$data['starttime']=time();
		    $result=$this->add($data);

		    $data1['user_id']=$result;//用户ID		

		    $data1['phone']=$data['phone'];
		    $data1['offical']=$code;
	     	$data1['shouquan']=$code;
	     	$data1['pw']=$authnum;
	     	$data1['level']=1;
		
		     $result1=M('relative')->add($data1);
        }
        else
        {
        	// return implode(';', array_keys($data));exit;
        	 $result=$this->where("id='$user_id'")->save($data);


             $data1=array();
             $data1['phone']=$data['phone'];
        	 $result1=M('relative')->where("user_id='$user_id'")->save($data1);

        	if($result)
        	return "更新成功";
           else
           	return "更新失败";
           exit();
        }



		


		


		/*	var_dump($this->getlastsql());*/
		if($result1){


			$this->mobile_code = $authnum;
			$this->name=$data['name'];
			$message = "【微百佳】".$this->name.",您已通过'润晟堂经销商授权',授权号为".$code.",初始密码为". $this->mobile_code.";请妥善保管" ;

			$sms_error = '';
			$mobile=$data['phone'];

			$target = "http://xtx.telhk.cn:8080/sms.aspx?action=send";
			$post_data =  "userid=6013&account=weibaijia&password=weiwei7788&mobile={$mobile}&content={$message}&sendTime=&extno=";
			$gets =  $this->xml_to_array(post_data($post_data, $target));
			if($gets['returnsms']['returnstatus']=="Success"){
				return "短信已发出，请注意查收！";
			}else{
				return "短信发送失败，请重发！";
			}
		}else{
			return "授权失败！";
		}

	}


	function add_user1(){//大区授权



		$data=I('');

		//更新操作
		$user_id=$data["user_id"];
		unset($data["user_id"]);




		//检测手机/微信是否存在
		$status=$this->check_user($data['phone'],$data['wechat'],$user_id);
		if($status){return "手机号或微信号已存在";exit;} 


        
		
        //判断官方是否修改
        $diff=1;
        if($user_id)
        {
        	$offical = M('relative')->where("user_id='$user_id'")->getField ( 'offical' );
        	if($offical==$data['shouquan'])$diff=0;
        }
		

        if($diff)
        {
        	$data2['offical']=$data['shouquan'];
			$code = M('relative')->distinct ( true )->order('zoon desc')->where($data2)->getField ( 'zoon' );
			$code=$code+1;
			if($code>99){
				return "该官方下大区名额已授权完！";
				exit;
			}
			if(mb_strlen($code)<2){
				$code="0".$code;
			}
			$code1=$code;
			$code=$data['shouquan'].$code;
		    $data['shouquan']=$code;

		    //授权时间
		    $data['starttime']=time();
			$todytime=date( "Y-m-d", $data['starttime']); 
			$year=date( "Y", $data['starttime']);
			$month=date( "m", $data['starttime']);
			$day=date( "d", $data['starttime']);

			if(intval($data['time'])<6 || intval($data['time'])>48){return "授权时间有误，必须是大于6小于48的数字";exit();}

			$Stime=$month+$data['time'];
			if($Stime>48){
				$year=$year+4;
				$month=$Stime-48;
				$endtime=$year.'-'.$month.'-'.$day;
			}
			else if($Stime>36&&$Stime<49){
				$year=$year+3;
				$month=$Stime-36;
				$endtime=$year.'-'.$month.'-'.$day;
			}
			else if($Stime>24&&$Stime<37){
				$year=$year+2;
				$month=$Stime-24;
				$endtime=$year.'-'.$month.'-'.$day;
			}
			else if($Stime>12&&$Stime<25){
				$year=$year+1;
				$month=$Stime-12;
				$endtime=$year.'-'.$month.'-'.$day;
			}
			else{
				$endtime=$year.'-'.$Stime.'-'.$day;
				
			}
			$data['endtime']=strtotime($endtime);
        }


		if(!$user_id)
        {
			$ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
			$list=explode(",",$ychar);
			for($i=0;$i<8;$i++){
				$randnum=rand(0,35); 
				$authnum.=$list[$randnum];
			}
			$data['password']=md5($authnum);

	

			
        }
        else
        {
        	$org_month = $this->where("user_id='$user_id'")->getField ( 'time' );
        	//相同则不改变时间
		   if($org_month!=$data['time'])
		   {
				$data['starttime']=time();
				$todytime=date( "Y-m-d", $data['starttime']); 
				$year=date( "Y", $data['starttime']);
				$month=date( "m", $data['starttime']);
				$day=date( "d", $data['starttime']);

				if(intval($data['time'])<6 || intval($data['time'])>48){return "授权时间有误，必须是大于6小于48的数字";exit();}

				$Stime=$month+$data['time'];
				if($Stime>48){
					$year=$year+4;
					$month=$Stime-48;
					$endtime=$year.'-'.$month.'-'.$day;
				}
				else if($Stime>36&&$Stime<49){
					$year=$year+3;
					$month=$Stime-36;
					$endtime=$year.'-'.$month.'-'.$day;
				}
				else if($Stime>24&&$Stime<37){
					$year=$year+2;
					$month=$Stime-24;
					$endtime=$year.'-'.$month.'-'.$day;
				}
				else if($Stime>12&&$Stime<25){
					$year=$year+1;
					$month=$Stime-12;
					$endtime=$year.'-'.$month.'-'.$day;
				}
				else{
					$endtime=$year.'-'.$Stime.'-'.$day;
					
				}
				$data['endtime']=strtotime($endtime);
		   }
		   else
		   {
		   	unset($data['starttime']);
		   	unset($data['endtime']);
		   	unset($data['time']);
		   }
        }

       



		

		if(!$user_id)
		{
            $result=$this->add($data);
         	$data1['user_id']=$result;//用户ID
			$data1['phone']=I('phone');
			$data1['offical']=I('shouquan');
			$data1['zoon']=$code1;
			$data1['shouquan']=$code;
			$data1['level']=2;
			$data1['pw']=$authnum;
			$result1=M('relative')->add($data1);

		}
		else
		{
			$result=$this->where("id='$user_id'")->save($data);

             $data1=array();
             $data1['phone']=$data['phone'];
             if($diff)
             {
             	$data1['offical']=I('shouquan');
				$data1['zoon']=$code1;
				$data1['shouquan']=$code;
             }
        	 $result1=M('relative')->where("user_id='$user_id'")->save($data1);

        	if($result)
        	return "更新成功";
           else
           	return "更新失败";
           exit();

		}
			
		





		if($result){
			$this->mobile_code = $authnum;
			$this->name=$data['name'];
			$message = "【微百佳】".$this->name.",您已通过'润晟堂经销商授权,授权号为'".$code.",初始密码为". $this->mobile_code."。请妥善保管" ;

			$sms_error = '';
			$mobile=$data['phone'];

			$target = "http://xtx.telhk.cn:8080/sms.aspx?action=send";
			$post_data =  "userid=6013&account=weibaijia&password=weiwei7788&mobile={$mobile}&content={$message}&sendTime=&extno=";
			$gets =  $this->xml_to_array(post_data($post_data, $target));
			if($gets['returnsms']['returnstatus']=="Success"){
				return "短信已发出，请注意查收！";
			}else{
				return "短信发送失败，请重发！";
			}
		}else{
			return "授权失败！";
		}
	}

	public function add_user2(){//总代理授权
		$data=I('');

		//更新操作
		$user_id=$data["user_id"];
		unset($data["user_id"]);



		//检测手机/微信是否存在
		$status=$this->check_user($data['phone'],$data['wechat'],$user_id);
		if($status){return "手机号或微信号已存在";exit;} 

		//判断官方是否修改
        $diff=1;
        if($user_id)
        {
        	$rv_info = M('relative')->field('offical,zoon')->where("user_id='$user_id'")->find();
        	if(($rv_info['offical']==$data['shouquan'])&&($rv_info['zoon']==$data['zoon']))$diff=0;
        }

        //relative构造
        $data1=array();
        //授权码是否需要变化
        if($diff)
        {
           	$data1['offical']=$data['shouquan'];
			$data1['zoon']=$data['zoon'];

			$code = M('relative')->distinct ( true )->order('agent desc')->where($data1)->getField ( 'agent' );
			$code=$code+1;
			if($code>99){
				return "该官方大区下总代理名额已授权完！";
				exit;
			}
			if(mb_strlen($code)<2){
				$code="0".$code;
			}
			$code1=$code;
			$code=$data['shouquan'].$data['zoon'].$code;

			$data['shouquan']=$code;
			$data1['agent']=$code1;
			$data1['shouquan']=$code;

			//授权时间
			$data['time']=3;
			$data['state']=4;
			$data['starttime']=time();

		
			$todytime=date( "Y-m-d", $data['starttime']); 
			$year=date( "Y", $data['starttime']);
			$month=date( "m", $data['starttime']);
			$day=date( "d", $data['starttime']);
			$Stime=$month+3;
			if($Stime>12){
				$year=$year+1;
				$month=$Stime-12;
				$endtime=$year.'-'.$month.'-'.$day;
			}else{
				$endtime=$year.'-'.$Stime.'-'.$day;
				
			}
			$data['endtime']=strtotime($endtime);


        }
		


		if(!$user_id)
		{


			$ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
			$list=explode(",",$ychar);
			for($i=0;$i<8;$i++){
				$randnum=rand(0,35); 
				$authnum.=$list[$randnum];
			}
			$data['password']=md5($authnum);
			/*$info=$this->where("shouquan='{$code}'")->count();
			while($info==1){
				$code=rand(01, 99);
				if(mb_strlen($code)<2){
					$code="0".$code;
				}
				$code1=$code;
				$code=$data('shouquan').$data('zoon').$code;
				$info=$this->where("shouquan='{$code}'")->count();
			}*/
			$data1['phone']=$data['phone'];


			
			
			if(!$this->create($data)){
				$this->getError();
			}else{
				$result=$this->add();
			}
			$data1['user_id']=$result;//用户ID

			$data1['level']=3;
			$data1['pw']=$authnum;
			$result1=M('relative')->add($data1);


		}
		else
		{
		
			// return implode(',', array_keys($data));exit;

			//构造用户数字
			$user_data=array();
		    $user_data['wechat']=$data['wechat'];
		    $user_data['phone']=$data['phone'];
		    $user_data['name']=$data['name'];
		    $user_data['sex']=$data['sex'];
		    $user_data['address']=$data['address'];
		    if($diff)
		    {

		    	$user_data['shouquan']=$data['shouquan'];

			    $user_data['time']=$data['time'];
		        $user_data['state']=$data['state'];//赋值4 --不知道
		        $user_data['starttime']=$data['starttime'];
		        $user_data['endtime']=$data['endtime'];	    	
		    }



		    $result=$this->where("id='$user_id'")->save($user_data);
			
            
             $data1['phone']=$data['phone'];
        	 $result1=M('relative')->where("user_id='$user_id'")->save($data1);

        	if($result)
        	return "更新成功";
           else
           	return "更新失败";
           exit();
		}
		


		
		if($result){
			$this->mobile_code = $authnum;
			$this->name=$data['name'];
			$message = "【微百佳】".$this->name.",您已通过润晟堂经销商授权,授权号为".$code.",初始密码为". $this->mobile_code."请妥善保管" ;

			$sms_error = '';
			$mobile=$data['phone'];

			$target = "http://xtx.telhk.cn:8080/sms.aspx?action=send";
			$post_data =  "userid=6013&account=weibaijia&password=weiwei7788&mobile={$mobile}&content={$message}&sendTime=&extno=";
			$gets =  $this->xml_to_array(post_data($post_data, $target));
			if($gets['returnsms']['returnstatus']=="Success"){
				return "短信已发出，请注意查收！";
			}else{
				return "短信发送失败，请重发！";
			}
		}else{
			return "授权失败！";
		}
	}

	function add_user3(){//普通代理授权
		$data=I('');


		//更新操作
		$user_id=$data["user_id"];
		unset($data["user_id"]);


		//检测手机/微信是否存在
		$status=$this->check_user($data['phone'],$data['wechat'],$user_id);
		if($status){return "手机号或微信号已存在";exit;} 

		//判断官方是否修改
        $diff=1;
        if($user_id)
        {
        	$rv_info = M('relative')->field('offical,zoon,agent')->where("user_id='$user_id'")->find();
        	if(($rv_info['offical']==$data['shouquan'])&&($rv_info['zoon']==$data['zoon'])&&($rv_info['agent']==$data['agent']))$diff=0;
        }

        //relative构造
        $data1=array();

        //授权有变更
        if($diff)
        {
        	//授权码生成
        	$data1['offical']=$data['shouquan'];
			$data1['zoon']=$data['zoon'];
			$data1['agent']=$data['agent'];
			$code = M('relative')->distinct ( true )->order('normal desc')->where($data1)->getField ( 'normal' );
			$code=$code+1;
			if($code>999){
				return "该官方大区总代下普通代理名额已授权完！";
				exit;
			}
			if(mb_strlen($code)<2){
				$code="00".$code;
			}
			else if(mb_strlen($code)<3)
		      {
		        $code="0".$code;
		      }
			$code1=$code;
			$code=$data['shouquan'].$data['zoon'].$data['agent'].$code;

			$data['shouquan']=$code;


			//授权时间
			$data['time']=3;
			$data['starttime']=time();

			$todytime=date( "Y-m-d", $data['starttime']); 
			$year=date( "Y", $data['starttime']);
			$month=date( "m", $data['starttime']);
			$day=date( "d", $data['starttime']);
			$Stime=$month+3;
			if($Stime>12){
				$year=$year+1;
				$month=$Stime-12;
				$endtime=$year.'-'.$month.'-'.$day;
			}else{
				$endtime=$year.'-'.$Stime.'-'.$day;
				
			}
			$data['endtime']=strtotime($endtime);

        }


		if(!$user_id)
		{
		/*		var_dump($data);*/
				
				/*$data['shouquan']=$code;*/
				$ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
				$list=explode(",",$ychar);
				for($i=0;$i<8;$i++){
					$randnum=rand(0,35); 
					$authnum.=$list[$randnum];
				}
				$data['password']=md5($authnum);
				/*$info=$this->where("shouquan='{$code}'")->count();
				while($info==1){
					$code=rand(01, 999);
					if(mb_strlen($code)<2){
						$code="00".$code;
					}
					$code=$data['shouquan'].$data['zoon'].$data['agent'].$code;
					$info=$this->where("shouquan='{$code}'")->count();
				}*/
				
				if(!$this->create($data)){
					$this->getError();
				}else{
					$result=$this->add();
				}
				$data1['user_id']=$result;//用户ID

				$data1['phone']=$data['phone'];
				/*		$data1['offical']=$data['shouquan'];*/
				$data1['normal']=$code1;
				$data1['shouquan']=$data['shouquan'];
				$data1['level']=4;
				$data1['pw']=$authnum;
				$result1=M('relative')->add($data1);
		}
		else
		{
			//构造用户数组
			$user_data=array();
		    $user_data['wechat']=$data['wechat'];
		    $user_data['phone']=$data['phone'];
		    $user_data['name']=$data['name'];
		    $user_data['sex']=$data['sex'];
		    $user_data['address']=$data['address'];
		    if($diff)
		    {

		    	$user_data['shouquan']=$data['shouquan'];

			    $user_data['time']=$data['time'];
		        $user_data['state']=$data['state'];//赋值4 --不知道
		        $user_data['starttime']=$data['starttime'];
		        $user_data['endtime']=$data['endtime'];	


		        $data1['normal']=$code1;
				$data1['shouquan']=$data['shouquan'];   	
		    }

			$result=$this->where("id='$user_id'")->save($user_data);


             $data1['phone']=$data['phone'];
        	 $result1=M('relative')->where("user_id='$user_id'")->save($data1);

        	if($result)
        	return "更新成功";
           else
           	return "更新失败";
           exit();
		}

		
		if($result){
			$this->mobile_code = $authnum;
			$this->name=$data['name'];
			$message = "【微百佳】".$this->name.",您已通过润晟堂经销商授权,授权号为".$code.",初始密码为". $this->mobile_code."请妥善保管" ;

			$sms_error = '';
			$mobile=$data['phone'];

			$target = "http://xtx.telhk.cn:8080/sms.aspx?action=send";
			$post_data =  "userid=6013&account=weibaijia&password=weiwei7788&mobile={$mobile}&content={$message}&sendTime=&extno=";
			$gets =  $this->xml_to_array(post_data($post_data, $target));
			if($gets['returnsms']['returnstatus']=="Success"){
				return "短信已发出，请注意查收！";
			}else{
				return "短信发送失败，请重发！";
			}
		}else{
			return "授权失败";
		}
	}
     


    //检测微信或手机是否已存在
	protected function check_user($mobile='',$wechat='',$user_id='')
	{
		$sm_p=1;
		$sm_w=1;
		if($user_id)
		{
           $data=$this->field('phone,wechat')->where("id='$user_id'")->find();

           if(($data['phone']==$mobile)&&$mobile)
           {$sm_p=0;}	
           if($data['wechat']==$wechat)
           {$sm_w=0;}	
		}

		if($mobile&&$sm_p) $info1=$this->where("phone='$mobile'")->getField("id");
	    if($wechat&&$sm_w) $info2=$this->where("wechat='$wechat'")->getField("id");


	    if($info1 || $info2)return true;
	    else return false;
	}

	protected function xml_to_array($xml){
		$reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
		if(preg_match_all($reg, $xml, $matches)){
			$count = count($matches[0]);
			for($i = 0; $i < $count; $i++){
				$subxml= $matches[2][$i];
				$key = $matches[1][$i];
				if(preg_match( $reg, $subxml )){
					$arr[$key] = $this->xml_to_array( $subxml );
				}else{
					$arr[$key] = $subxml;
				}
			}
		}
		return $arr;
	}
}